package edu.bit.water.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.LabelEvaEntPrdInfoDao;
import edu.bit.water.entity.LabelEvaEntPrdInfo;
/**
 * 标签表的业务层
 * @author HJS
 *
 */
@Service
public class LabelEvaEntPrdInfoService {
	
	@Autowired
	private LabelEvaEntPrdInfoDao labelEvaEntPrdInfoDao;
	@Autowired
	private EntityEvaEntPrdInfoService entityEvaEntPrdInfoService;
	
	/**
	 * 查询标签表中的记录
	 * @return 标签表中的所有记录
	 */
	public List<LabelEvaEntPrdInfo> select(){
		List<LabelEvaEntPrdInfo> list = labelEvaEntPrdInfoDao.select();
		return list;
	}
	
	/**
	 * 插入记录
	 * @param labelEvaEntPrdInfo 标签记录实体
	 * @return 成功返回1,失败返回0	 
	 */
	public Map<String, String> insert(LabelEvaEntPrdInfo labelEvaEntPrdInfo) {
		Map<String, String> modelMap = new HashMap<>();
		try {
			int n = labelEvaEntPrdInfoDao.insert(labelEvaEntPrdInfo);
			if (n == 1)
				modelMap.put("status", "success");
			else 
				modelMap.put("status", "failed");
			return modelMap;
		} catch (DuplicateKeyException e) {
			// TODO: handle exception
			if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "创建时间已存在，请重新输入");
				return modelMap;
			}
		}
		return modelMap;
	}
	/**
	 * 更新记录
	 * @param labelEvaEntPrdInfo 标签记录实体
	 * @return 成功返回1,失败返回0	 
	 */
	public Map<String, String> update(LabelEvaEntPrdInfo labelEvaEntPrdInfo) {
		Map<String, String> modelMap = new HashMap<>();
		try {
			int n = labelEvaEntPrdInfoDao.update(labelEvaEntPrdInfo);
			if (n == 1)
				modelMap.put("status", "success");
			else 
				modelMap.put("status", "failed");
			return modelMap;
		} catch (DuplicateKeyException e) {
			// TODO: handle exception
			if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "创建时间已存在，请重新输入");
				return modelMap;
			}
		}
		return modelMap;
	}
	/**
	 * 删除记录
	 * @param id 删除记录的标识符
	 * @return 成功返回1,失败返回0	 
	 */
	public int delete(List<Integer> ids) {
		//1.首先删除生产情况实体表中的记录，如果删除成功才能删除标签表中的记录
		try {
			for (Integer id : ids) {
				entityEvaEntPrdInfoService.deleteFd(id);
				labelEvaEntPrdInfoDao.delete(id);
			}
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}	
}
